// DlgEditLinks.cpp : implementation file
//

#include "stdafx.h"
#include "Celsus.h"
#include "DlgEditLinks.h"
#include "DlgEditText.h"


// CDlgEditLinks dialog

IMPLEMENT_DYNAMIC(CDlgEditLinks, CDialog)

CDlgEditLinks::CDlgEditLinks(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgEditLinks::IDD, pParent)
{
	m_nStaffID = -1;
	m_bSortAscending = TRUE;
	m_nCurrentSort = 0;
}

CDlgEditLinks::~CDlgEditLinks()
{
}

void CDlgEditLinks::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_LINKS, m_ctrlLinksList);
}


BEGIN_MESSAGE_MAP(CDlgEditLinks, CDialog)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_LINKS, &CDlgEditLinks::OnLvnItemchangedListLinks)
	ON_NOTIFY(LVN_GETDISPINFO, IDC_LIST_LINKS, &CDlgEditLinks::OnLvnGetdispinfoListLinks)
	ON_NOTIFY(HDN_ITEMCLICK, 0, &CDlgEditLinks::OnHdnItemclickListLinks)
	ON_NOTIFY(LVN_ODFINDITEM, IDC_LIST_LINKS, &CDlgEditLinks::OnLvnOdfinditemListLinks)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_LINKS, &CDlgEditLinks::OnNMDblclkListLinks)
END_MESSAGE_MAP()


// CDlgEditLinks message handlers

BOOL CDlgEditLinks::OnInitDialog()
{
	CDialog::OnInitDialog();

	ASSERT( m_nStaffID >= 0 );

	// set extended styles
	m_ctrlLinksList.SetExtendedStyle( LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_SUBITEMIMAGES | LVS_EX_INFOTIP  );
	// insert columns
	m_ctrlLinksList.InsertColumn( LINK_UID, _T("Identifier"), LVCFMT_CENTER,  200, LINK_UID );
	m_ctrlLinksList.InsertColumn( LINK_SOURCE, _T("Source"), LVCFMT_LEFT,  200, LINK_SOURCE );	
	m_ctrlLinksList.InsertColumn( LINK_URL, _T("Address"), LVCFMT_LEFT,  200, LINK_URL );

	// fill list
	FillList();

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgEditLinks::OnOK()
{
	// TODO: Add your specialized code here and/or call the base class

	CDialog::OnOK();
}

void CDlgEditLinks::OnCancel()
{
	// TODO: Add your specialized code here and/or call the base class

	CDialog::OnCancel();
}

void CDlgEditLinks::FillList()
{
	// clear lists
	m_aLinksList.RemoveAll();
	m_ctrlLinksList.DeleteAllItems();

	// get links
	appConfig.m_SQL.GetRequestLinkss( &m_aLinksList, m_szFilter, m_szSort );

	// set item count
	m_ctrlLinksList.SetItemCountEx( m_aLinksList.GetCount(), LVSICF_NOINVALIDATEALL | LVSICF_NOSCROLL );

	// set column width		
	m_ctrlLinksList.SetColumnWidth( LINK_UID, LVSCW_AUTOSIZE );
	//m_ctrlLinksList.SetColumnWidth( LINK_SOURCE, LVSCW_AUTOSIZE );
	m_ctrlLinksList.SetColumnWidth( LINK_URL, LVSCW_AUTOSIZE );
}

void CDlgEditLinks::OnLvnItemchangedListLinks(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);

	// get new selection
	if( pNMLV->uNewState & LVIS_SELECTED )
	{
	}
	*pResult = 0;
}

void CDlgEditLinks::OnLvnGetdispinfoListLinks(NMHDR *pNMHDR, LRESULT *pResult)
{
	NMLVDISPINFO *pDispInfo = reinterpret_cast<NMLVDISPINFO*>(pNMHDR);
	// TODO: Add your control notification handler code here
	*pResult = 0;

    // Create a pointer to the item
    LV_ITEM* pItem = &(pDispInfo)->item;

    // Which item number?
    INT nItemId = pItem->iItem;

	// get request
	ASSERT( nItemId <= m_aLinksList.GetCount() );
	REQUEST_LINK currentLink;
	currentLink = m_aLinksList.GetAt( nItemId );

	// Does the list need text information?
	if( pItem->mask & LVIF_TEXT )
	{
		TRACE( _T("SubItem = %d\n"), pItem->iSubItem );

		// set text
		CString szText;
		// set item id
		switch( pItem->iSubItem )		
		{
		case LINK_UID:
			szText = currentLink.m_uid;
			break;
		case LINK_SOURCE:
			szText = currentLink.m_source;
			break;
		case LINK_URL:
			szText = currentLink.m_link;
			break;
		}
		//Copy the text to the LV_ITEM structure
		//Maximum number of characters is in pItem->cchTextMax
		lstrcpyn( pItem->pszText, szText, pItem->cchTextMax );
	}

	// Does the list need image information?
	if( pItem->mask & LVIF_IMAGE ) 
	{
		TRACE( _T("IMAGE\n") );
		//// insert image
		//INT nImage = GetItemImage( nItemId, pItem->iSubItem );
		//// set image
		//if( nImage >= 0 )
		//	pItem->iImage = nImage;
	}

	// return
	*pResult = CDRF_DODEFAULT;
	*pResult = 0;
}

void CDlgEditLinks::OnHdnItemclickListLinks(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMHEADER phdr = reinterpret_cast<LPNMHEADER>(pNMHDR);

	HDITEM hdi;
	CHeaderCtrl *pHeader = m_ctrlLinksList.GetHeaderCtrl();
	ASSERT( pHeader );

	// toggle sort
	if( m_nCurrentSort == phdr->iItem )
		m_bSortAscending = !m_bSortAscending;
	else
	{
		// set sort type
		m_bSortAscending = TRUE;
		// set header information
		memset( &hdi, NULL, sizeof( HDITEM ));
		pHeader->GetItem( m_nCurrentSort, &hdi );
		hdi.mask |= HDI_FORMAT;
		hdi.fmt |= HDF_STRING;
		pHeader->SetItem( m_nCurrentSort, &hdi );
	}
	// set current sort
	m_nCurrentSort = phdr->iItem;

	// check for sort
	switch( m_nCurrentSort )
	{
	case LINK_UID:
		m_szSort = _T("uid");
		break;
	case LINK_SOURCE:
		m_szSort = _T("source");
		break;
	case LINK_URL:
		m_szSort = _T("link");
		break;
	default:
		*pResult = 0;
		return;
	}

	// set sort type
	if( m_bSortAscending )
		m_szSort += _T(" ASC");
	else
		m_szSort += _T(" DESC");

	// set header information
	if( m_nCurrentSort >= 0 )
	{
		memset( &hdi, NULL, sizeof( HDITEM ));
		pHeader->GetItem( m_nCurrentSort, &hdi );
		hdi.mask |= HDI_FORMAT;
		if( m_bSortAscending )
			hdi.fmt |= HDF_STRING | HDF_SORTUP;
		else
			hdi.fmt |= HDF_STRING | HDF_SORTDOWN;
		pHeader->SetItem( m_nCurrentSort, &hdi );
	}

	// fill list
	FillList();

	*pResult = 0;
}

void CDlgEditLinks::OnLvnOdfinditemListLinks(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMLVFINDITEM pFindInfo = reinterpret_cast<LPNMLVFINDITEM>(pNMHDR);
	// TODO: Add your control notification handler code here
	*pResult = 0;
}

void CDlgEditLinks::OnNMDblclkListLinks(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	
    // Which item number?
    INT nItemId = pNMItemActivate->iItem;

	if( nItemId >= 0 )
	{
		// get request
		ASSERT( nItemId <= m_aLinksList.GetCount() );
		REQUEST_LINK currentLink;
		currentLink = m_aLinksList.GetAt( nItemId );

		CDlgEditText dlgText;
		dlgText.m_szTitle = _T("Edit Address URL");
		dlgText.m_szLabel = _T("Address:");
		dlgText.m_szText = currentLink.m_link;
		dlgText.m_bShowOpenButton = TRUE;
		// show dialog
		if( dlgText.DoModal() == IDOK )
		{
			// update link
			if( dlgText.m_szText.IsEmpty() )
			{
				AfxMessageBox( _T("You must specify a link address.") );
			}
			else
			{
				currentLink.m_link = dlgText.m_szText;
				currentLink.m_staff_id = m_nStaffID;
				appConfig.m_SQL.SetRequestLinks( &currentLink );
				// fill list
				FillList();
			}
		}
	}	
	*pResult = 0;
}
